#!/usr/bin/env python
# -*- coding: latin-1 -*-

# 2009-05-13.py
# Python(x,y) automatic logging file


#   15:50
# =================================
from numpy import arange, exp, sign, squeeze, random, abs
import pylab
from lrange import lrange

def Fc(m=1,n=1,q=1):
    return m*exp(-n*q)

def Fy(u,T):
    return sign(u-0.5)*T*((1+1.0/T)**abs(2*u-1)-1.0)

def Fy(T0,m,n,k,q):
    u = squeeze(random.uniform(0.0, 1.0))
    return sign(u-0.5)*Ft(T0,m,n,k,q)*((1+1.0/Ft(T0,m,n,k,q))**abs(2*u-1)-1.0)

def Fp(dE,T,b=1):
    return exp(-dE*1.0/b/T)

def Ft(T0,c,k,q):
    return T0*exp(-c * k**(q))

def Ft(T0,m,n,k,q):
    return T0*exp(-(m*exp(-n*q)) * k**(q))

def test_q():
    """
    Test the effect of varying the quenching parameter over a range of 10 cooling iterations.
    """
    pylab.ioff()
    k = arange(10)
    pylab.figure()
    # q = 1.0
    pylab.subplot(411)
    for i in range(30):
        pylab.plot(k,Fy(1,1,1,k,1.0))
    pylab.title('q = 1.0, m = 1.0, n = 1.0')
    pylab.xlabel('k')
    pylab.ylabel('y')
    
    # q = 1.5
    pylab.subplot(412)
    for i in range(30):
        pylab.plot(k,Fy(1,1,1,k,1.5))
    pylab.title('q = 1.5, m = 1.0, n = 1.0')
    pylab.xlabel('k')
    pylab.ylabel('y')
    
    # q = 2.0
    pylab.subplot(413)
    for i in range(30):
        pylab.plot(k,Fy(1,1,1,k,2.0))
    pylab.title('q = 2.0, m = 1.0, n = 1.0')
    pylab.xlabel('k')
    pylab.ylabel('y')
    
    # q = 2.5
    pylab.subplot(414)
    for i in range(30):
        pylab.plot(k,Fy(1,1,1,k,2.5))
    pylab.title('q = 2.5, m = 1.0, n = 1.0')
    pylab.xlabel('k')
    pylab.ylabel('y')
    
    pylab.ion()
    pylab.show()

    
def test_m():
    """
    Test the effect of varying the m parameter over a range of 10 cooling iterations.
    """
    pylab.ioff()
    k = arange(10)
    pylab.figure()
    # m=0.25
    pylab.subplot(411)
    for i in range(30):
        pylab.plot(k,Fy(1,.25,1,k,2.0))
    pylab.title('q = 2.0, m = .25, n = 1.0')
    pylab.xlabel('k')
    pylab.ylabel('y')
    
    # m=0.5
    pylab.subplot(412)
    for i in range(30):
        pylab.plot(k,Fy(1,.5,1,k,2.0))
    pylab.title('q = 2.0, m = .5, n = 1.0')
    pylab.xlabel('k')
    pylab.ylabel('y')
    
    # m=1.0
    pylab.subplot(413)
    for i in range(30):
        pylab.plot(k,Fy(1,1.0,1,k,2.0))
    pylab.title('q = 2.0, m = 1.0, n = 1.0')
    pylab.xlabel('k')
    pylab.ylabel('y')
    
    # m=1.5
    pylab.subplot(414)
    for i in range(30):
        pylab.plot(k,Fy(1,1.5,1,k,2.0))
    pylab.title('q = 2.0, m = 1.5, n = 1.0')
    pylab.xlabel('k')
    pylab.ylabel('y')
    
    pylab.ion()
    pylab.show()
    
def test_n():
    """
    Test the effect of varying the m parameter over a range of 10 cooling iterations.
    """
    pylab.ioff()
    k = arange(10)
    pylab.figure()
    # n=0.25
    pylab.subplot(411)
    for i in range(30):
        pylab.plot(k,Fy(1,1,.25,k,2.0))
    pylab.title('q = 2.0, m = 1.0, n = .25')
    pylab.xlabel('k')
    pylab.ylabel('y')
    
    # n=0.5
    pylab.subplot(412)
    for i in range(30):
        pylab.plot(k,Fy(1,1,.5,k,2.0))
    pylab.title('q = 2.0, m = 1.0, n = .5')
    pylab.xlabel('k')
    pylab.ylabel('y')
    
    # m=1.0
    pylab.subplot(413)
    for i in range(30):
        pylab.plot(k,Fy(1,1.0,1,k,2.0))
    pylab.title('q = 2.0, m = 1.0, n = 1.0')
    pylab.xlabel('k')
    pylab.ylabel('y')
    
    # m=1.5
    pylab.subplot(414)
    for i in range(30):
        pylab.plot(k,Fy(1,1,1.5,k,2.0))
    pylab.title('q = 2.0, m = 1, n = 1.5')
    pylab.xlabel('k')
    pylab.ylabel('y')
    
    pylab.ion()
    pylab.show()
    
def plot_params(T0=1,m=1,n=1,q=1,iter=10,r=1):
    """
    Test the effect of any set of parameters over a range of 10 cooling iterations.
    r is the step size of the parameter of interest.
    """
    pylab.ioff()
    k = arange(iter)
    pylab.figure()
    # n=0.25
    pylab.subplot(111)
    for i in range(60):
        pylab.plot(k,r*Fy(T0,m,n,k,q),'b-')
    pylab.title('T0 = '+str(T0)+', q = '+str(q)+', m = '+str(m)+', n = '+str(n)+', Step Size = '+str(r))
    pylab.xlabel('k')
    pylab.ylabel('y')
    pylab.ion()
    pylab.show()
  
def plot_step_size(T0=1,m=1,n=1,q=1,rscale=1):
    """
    Test the effect of any set of parameters over a range of 10 cooling iterations.
    rscale is the step size scale of the parameter of interest r = [0,2,0.1)*rscale.
    """
    r = arange(0,2,.1)
    r = r*rscale
    pylab.ioff()
    pylab.figure()
    # n=0.25
    clr = ['b','g','r','c','m','y','k']
    clr = ['.1','.2','.3','.4','.5','.6','.7']
    pylab.subplot(111)
    for k in range(7):
        for i in range(10):
            pylab.plot(r,r*Fy(T0,m,n,k,q),clr[k])
            
    pylab.title('T0 = '+str(T0)+', q = '+str(q)+', m = '+str(m)+', n = '+str(n))
    pylab.xlabel('Step Size')
    pylab.ylabel('y')
    pylab.ion()
    pylab.show()